package cn.pn.project.controller;


import cn.pn.project.client.base.CityPositioningFeign;
import cn.pn.project.common.DtoUtil;

import cn.pn.project.pojo.base.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cityPositioning1")
public class CityPositioningController {

    @Autowired
    private CityPositioningFeign cityPositioningFeign;

    /**
     * 通过城市名称查询城市信息
     *
     * @return 全部城市集合
     */
    @PostMapping(value = {"/p/findCity"})
    public Object findCityBycityName(String cityName) {
        System.out.println("===========================================" + cityName);
        if (cityName == null) {
            return DtoUtil.returnFail("参数缺失", "6000");
        }
            Object o = cityPositioningFeign.findCityBycityName(cityName);
        if (o != null) {
            return DtoUtil.returnDataSuccess(o);
        } else {
            return DtoUtil.returnFail("获取数据失败", "0002");
        }
    }

    /**
     * 查询所有
     * @return
     * @throws Exception
     */
    @PostMapping(value = {"/p/getAllInfo"})
    public Object listCity() throws  Exception{
        List<City> list = cityPositioningFeign.listCity();
        if (list != null) {
            return DtoUtil.returnDataSuccess(list);
        } else {
            return DtoUtil.returnFail("获取数据失败", "0002");
        }
    }

    /**
     * 查询销售量前10的城市
     * @return
     * @throws Exception
     */
    @PostMapping(value = {"/p/getInfoTopTen"})
    public Object listCityTopTen() throws  Exception{
        List<City> list = cityPositioningFeign.listCityTopTen();
        System.out.println(list.get(0).getCityId());
        if (list != null) {
            return DtoUtil.returnDataSuccess(list);
        } else {
            return DtoUtil.returnFail("获取数据失败", "0002");
        }
    }

    @PostMapping("/p/findAll")
    public Object findAll() throws Exception {
        return DtoUtil.returnDataSuccess(cityPositioningFeign.findAll());
    }
}
